#!/bin/bash
dir_sql="/wlz/"
dir_log="/wlz/"
etl_date="`date -d '-1 days' +"%Y-%m-%d"`"
log_file="$dir_log/DWD_$(date +%Y%m%d).log"

echo "======================================" >> "$log_file"
start_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "执行开始: $start_time " >> "$log_file"
echo "将处理目录: $dir_sql 下的所有SQL文件" >> "$log_file"
echo "参数 etl_date 值为: $etl_date" >> "$log_file"

for sql_file in "$dir_sql"/*.sql
 do
    if [ -f "$sql_file" ]
    then
        echo "文件存在" >> "$log_file"
        echo "开始时间: $(date "+%Y-%m-%d %H:%M:%S")" >> "$log_file"

        hive --hivevar etl_date="$etl_date" -f "$sql_file" >> "$log_file" 2>&1

        file_name=$(basename "$sql_file")
        if [ $? -eq 0 ]; then
            echo "执行成功:$file_name" >> "$log_file"
        else
            echo "执行失败:$file_name" >> "$log_file"
        fi

        echo "结束时间: $(date "+%Y-%m-%d %H:%M:%S")" >> "$log_file"
    else exits
    fi
done
